package com.gsm.service.impl;

import com.gsm.dao.SubjectDao;
import com.gsm.entity.Result;
import com.gsm.entity.SysRole;
import com.gsm.entity.SysUser;
import com.gsm.po.Subject;
import com.gsm.service.SubjectService;
import com.gsm.utils.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 科目(Subject)表服务实现类
 */
@Service
public class SubjectServiceImpl implements SubjectService {
    @Autowired
    private SubjectDao subjectDao;
    @Autowired
    private IdUtils idUtils;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    @Override
    public Result insertSubject(Subject subject, SysUser u) {
        int showOrder = subjectDao.selectMaxShowOrder();
        long subjectId = idUtils.nextId();
        if (true) {
            subject.setSubjectId(subjectId);
            subject.setSubTotal(0);
            subject.setSubScore(5.0);
            subject.setShowOrder(++showOrder);
            subject.setStatus(1);
            subject.setCreateTime(new Date());
        }
        subjectDao.insertSubject(subject);
        return Result.success();
    }

    /**
     * 根据 主键 subjectId 删除
     *
     * @param subjectId 主键
     * @param u
     * @return
     */
    @Override
    public Result deleteSubjectBySubjectId(Long subjectId, SysUser u) {
        subjectDao.deleteSubjectBySubjectId(subjectId);
        return Result.success();
    }

    /**
     * 修改
     *
     * @param subject 实例对象
     * @param u
     * @return
     */
    @Override
    public Result updateSubjectBySubjectId(Subject subject, SysUser u) {
        subjectDao.updateSubjectBySubjectId(subject);
        return Result.success();
    }

    /**
     * 根据 id 查询单条数据
     *
     * @param subjectId 主键
     * @return
     */
    @Override
    public Result selectSubjectBySubjectId(Long subjectId) {
        Subject subject = subjectDao.selectSubjectBySubjectId(subjectId);
        Result result = Result.success(subject);
        return result;
    }

    /**
     * 根据条件查询列表
     *
     * @param subject 实例对象
     * @param u
     * @return
     */
    @Override
    public Result selectSubjectList(Subject subject, SysUser u) {
        boolean flag = true;
        List<SysRole> roles = u.getRoleList();
        if (roles != null) {
            for (SysRole i : roles) {
                if ("教师".equals(i.getRoleName()) && roles.size() == 1) {
                    flag = false;
                }
            }
        }
        //flag false 说明是教师权限
        if (!flag) {
            subject.setCreateUser(u.getUserId());
        }
        //没有对教师权限做限制
        List<Subject> list = subjectDao.selectSubjectList(subject);
        return Result.success(list);
    }

    @Override
    public Result updateSubjectShowOrder(List<Long> subjectIds, SysUser u) {
        subjectDao.updateSubjectShowOrder(subjectIds);
        return Result.success();
    }

    @Override
    public Result refresh() {
        redisTemplate.delete("web:subject:list");
        redisTemplate.delete("web:subject:list:zero");
        return Result.success();
    }

    @Override
    public Result refreshBySubjectId(Long subjectId) {
        redisTemplate.delete("web:subject:id:" + subjectId);
        redisTemplate.delete("web:chapters:courses:id:" + subjectId);
        return Result.success();
    }
}